package com.css.common.core.config.valid;

import com.css.common.core.annotation.ValiDateFormat;
import com.css.common.core.utils.BaseUtil;
import com.css.common.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.time.format.DateTimeFormatter;

/**
 * @description: 日期格式合法性校验器
 * @Author chenhui
 * @Date 2024/11/12 15:41
 */
@Slf4j
public class DateFormatValidator implements ConstraintValidator<ValiDateFormat,String> {
        private ValiDateFormat ann;
        @Override
        public void initialize(ValiDateFormat ann) {//初始化方法
            this.ann=ann;
        }
        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            if(BaseUtil.isNull(value)) return true;
            return DateUtils.checkDateFormat(value, DateTimeFormatter.ofPattern(ann.format()));
        }
}